//
//  ClubJoinedStrategyView.swift
//  ClubanGame
//
//  Created by Apple on 2024/8/17.
//  Copyright © 2024 94Wan. All rights reserved.
//

import UIKit

class ClubJoinedStrategyView: UIView, UICollectionViewDelegate, UICollectionViewDataSource {

    private let bgView = c_make_ex(UIView.self)
        .bgColor(.black.alpha(0.36)).end()
    private let whiteView = c_make_ex(UIView.self)
        .bgColor(.white).corner(12.0.Scale).end()
    private let titleLabel = c_make_ex(UILabel.self)
        .text("我加入的论坛").font(13.font_semiBold)
        .color(.black).end()
    private let lineView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("Square_forum_line").end()
    private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    
    var sourceData: [ClubGameCommunityAllListModel]? {
        didSet{
            collectionView.reloadData()
        }
    }
    
    var removeView: (() -> Void)?
    var toStrategyDetailVC: ((_ model: ClubGameCommunityAllListModel) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        subviews{
            bgView
            whiteView.subviews{
                titleLabel
                lineView
                collectionView
            }
        }
        
        
        let width = (295.0.Scale - 56.0.Scale)/4
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 8
        layout.minimumInteritemSpacing = 8
        layout.scrollDirection = .vertical
        layout.itemSize = CGSize(width: width, height: 66.0.Scale)
        layout.sectionInset = UIEdgeInsets(top: 0, left: 16.0.Scale, bottom: 0, right: 16.0.Scale)
        
        collectionView.collectionViewLayout = layout
        collectionView.backgroundColor = .clear
        collectionView.register(ClubJoinedStrategyItemCell.self, forCellWithReuseIdentifier: ClubJoinedStrategyItemCell.description())
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.delegate = self
        collectionView.dataSource = self
        
        bgView.fillContainer()
        whiteView.width(295.0.Scale).height(445.0.Scale).centerVertically().centerHorizontally()
        titleLabel.leading(24.0.Scale).top(16.0.Scale)
        lineView.leading(24.0.Scale).trailing(24.0.Scale).height(1).top(40.0.Scale)
        collectionView.leading(0).trailing(0).bottom(0).top(52.0.Scale)
        
        let tapG = UITapGestureRecognizer(target: self, action: #selector(dismissView))
        bgView.isUserInteractionEnabled = true
        bgView.addGestureRecognizer(tapG)
    }
    
    @objc private func dismissView(){
        removeView?()
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return sourceData?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ClubJoinedStrategyItemCell.description(), for: indexPath) as? ClubJoinedStrategyItemCell else { return UICollectionViewCell() }
        cell.model = sourceData?[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        guard let md = sourceData?[indexPath.row] else { return }
        toStrategyDetailVC?(md)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

//MARK: 图文分类cell
class ClubJoinedStrategyItemCell: UICollectionViewCell {
    
    private let iconImageView = c_make_ex(UIImageView.self)
        .corner(8.0.Scale).end()
    private let shadowView = c_make_ex(UIView.self)
        .bgColor(.black.alpha(0.1)).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color("#7B7D80").font(11.font_medium).alignment(.center).end()
    
    var model: ClubGameCommunityAllListModel?{
        didSet{
            guard let md = model else { return }
            nameLabel.text = md.name
            if md.isLocal {
                shadowView.isHidden = true
                iconImageView.image = md.icon.toImage
            }else{
                shadowView.isHidden = false
                iconImageView.loadImage(url: md.icon)
            }
        }
    }
        
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.backgroundColor = .clear
        backgroundColor = .clear
        
        contentView.subviews{
            iconImageView.subviews{
                shadowView
            }
            nameLabel
        }
        
        iconImageView.top(0).size(48.0.Scale).centerHorizontally()
        shadowView.fillContainer()
        nameLabel.leading(0).trailing(0).centerHorizontally()
        nameLabel.Top == iconImageView.Bottom + 4.0.Scale
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
